/**
 *  The Yz.Session prototype
 */
(function(Yz) {
    if ( typeof(Yz) != "function" ) {
        throw "Failed to identify the root Yz object";
    }

    // prevent multiple execution
    if ('Session' in Yz) {
        return;
    }

    Yz.Session = function()
    {
        //return this.init();
    };


    /****************************
     *      Implementation      *
     ****************************/

    Yz.Session.expDate = 0;
    Yz.Session.timeout = 0;

    Yz.Session.startTimer = function(startSecs, secsLeft)
    {
        console.log("start timer");
        var self = Yz.Session;
        self.outputTimer(new Date(startSecs*1000), secsLeft);
        self.expDate = new Date( (startSecs+secsLeft)*1000 );
        self.timeout = window.setInterval(
            self.outputTimer,
            1000
        );
    }

    Yz.Session.outputTimer = function()
    {
        var d = new Date();
        $('#yz-admin-time').text(d.toLocaleString());
        var delta = Math.floor( (Yz.Session.expDate - d.getTime())/1000 );
        if (delta >= 0) {
            var hr = Math.floor(delta/3600);
            var min = Math.floor( (delta%3600) / 60 );
            var sec = delta%60;
            $('#yz-admin-session-expires').text(
                hr+":"+
                (min<10 ? "0"+min : min)+":"+
                (sec<10 ? "0"+sec : sec)
            );
        }
    }

})(window.Yz);
